package com.lookme.lmtool.utils;

import java.lang.reflect.Field;

/**
 * 反射字段赋值工具类
 * @author blake
 */
public class ReflectUtils {
        /**
         * 将obj对象的field属性设置值为value
         * @param obj   对象
         * @param field 属性
         * @param value 值
         */
        public static void setFieldValue(Object obj, Field field, String value)  {
            try {
                field.setAccessible(true);
                if (field.getType() == java.lang.Integer.class||field.getType()==int.class) {
                    Double dd=new Double(value);
                    field.set(obj,dd.intValue() );
                } else if (field.getType() == java.lang.Short.class||field.getType()==short.class) {
                    field.set(obj, Short.parseShort(value));
                } else if (field.getType() == java.lang.Byte.class||field.getType()==byte.class) {
                    field.set(obj, Byte.parseByte(value));
                } else if (field.getType() == java.lang.Long.class||field.getType()==long.class) {
                    field.set(obj, Long.parseLong(value));
                } else if (field.getType() == java.lang.Double.class||field.getType()==double.class) {
                    field.set(obj, Double.parseDouble(value));
                } else if (field.getType() == java.lang.Float.class||field.getType()==float.class) {
                    field.set(obj, Float.parseFloat(value));
                }else if (field.getType() == java.lang.Boolean.class||field.getType()==boolean.class) {
                    field.set(obj, Boolean.parseBoolean(value));
                }  else {
                    field.set(obj, field.getType().cast(value));
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }

    }
